﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Master_CommonDictionarySrhForm
 * 类型：       控制层
 * 功能说明：   数据字典维护检索页面
 * 作者：       王伟栋
 * 创建日期：   2007/08/21
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Commons.Utils;

public partial class App_Master_CommonDictionarySrhForm : System.Web.UI.Page
{
    private const string COMMAND_SEARCH = "DoSearch";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            // 接收传递参数并初始化控件
            txtDictNo.Text = Convert.ToString(Context.Items["InpDictNo"]);
            txtCatalog.Text = Convert.ToString(Context.Items["InpCatalog"]);
            txtTitle.Text = Convert.ToString(Context.Items["InpTitle"]);
            txtContent.Text = Convert.ToString(Context.Items["InpContent"]);

            // 接收传递参数为检索条件
            ViewState["ParamDictNo"] = Convert.ToString(Context.Items["ParamDictNo"]);
            ViewState["ParamCatalog"] = Convert.ToString(Context.Items["ParamCatalog"]);
            ViewState["ParamTitle"] = Convert.ToString(Context.Items["ParamTitle"]);
            ViewState["ParamContent"] = Convert.ToString(Context.Items["ParamContent"]);
            ViewState["ParamCommand"] = Convert.ToString(Context.Items["ParamCommand"]);

            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                GridView1.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        // 传递控件输入内容
        Context.Items["InpDictNo"] = txtDictNo.Text;
        Context.Items["InpCatalog"] = txtCatalog.Text;
        Context.Items["InpTitle"] = txtTitle.Text;
        Context.Items["InpContent"] = txtContent.Text;

        // 传递检索条件
        Context.Items["ParamDictNo"] = Convert.ToString(ViewState["ParamDictNo"]);
        Context.Items["ParamCatalog"] = Convert.ToString(ViewState["ParamCatalog"]);
        Context.Items["ParamTitle"] = Convert.ToString(ViewState["ParamTitle"]);
        Context.Items["ParamContent"] = Convert.ToString(ViewState["ParamContent"]);
        Context.Items["ParamCommand"] = Convert.ToString(ViewState["ParamCommand"]);

        // 传递分页编号
        Context.Items["ParamPageIndex"] = GridView1.PageIndex;

        // 页面跳转至更新画面
        // Server.Transfer("~/App/Master/CommonDictionaryInsForm.aspx", true);
        ControllerUtils.Transfer(this.Context, "~/App/Master/CommonDictionaryInsForm.aspx", true);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        // 保存控件状态为检索条件
        ViewState["ParamDictNo"] = txtDictNo.Text;
        ViewState["ParamCatalog"] = txtCatalog.Text;
        ViewState["ParamTitle"] = txtTitle.Text;
        ViewState["ParamContent"] = txtContent.Text;
        ViewState["ParamCommand"] = COMMAND_SEARCH;
        GridView1.PageIndex = 0;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (COMMAND_SEARCH.Equals(ViewState["ParamCommand"]))
        {
            // 构建检索条件对象
            CommonDictionaryBean dataBean = new CommonDictionaryBean();
            dataBean.DictNo = Convert.ToString(ViewState["ParamDictNo"]);
            dataBean.Catalog = Convert.ToString(ViewState["ParamCatalog"]);
            dataBean.Title = Convert.ToString(ViewState["ParamTitle"]);
            dataBean.Content = Convert.ToString(ViewState["ParamContent"]);

            // 检索结果绑定到控件
            CommonDictionaryDao dao = new CommonDictionaryDao();
            IList dataList = dao.FindByCommonDictionary(dataBean);
            GridView1.DataSource = dataList;
            GridView1.DataBind();

            tblResult.Visible = dataList.Count > 0;
        }
        else
        {
            tblResult.Visible = false;
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        GridView1.PageIndex = e.NewPageIndex;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        // 传递参数
        int rowNum = int.Parse(e.CommandArgument.ToString());
        Context.Items["DictNo"] = GridView1.DataKeys[rowNum]["DictNo"];
        Context.Items["Catalog"] = GridView1.DataKeys[rowNum]["Catalog"];

        // 传递控件输入内容
        Context.Items["InpDictNo"] = txtDictNo.Text;
        Context.Items["InpCatalog"] = txtCatalog.Text;
        Context.Items["InpTitle"] = txtTitle.Text;
        Context.Items["InpContent"] = txtContent.Text;

        // 传递检索条件
        Context.Items["ParamDictNo"] = Convert.ToString(ViewState["ParamDictNo"]);
        Context.Items["ParamCatalog"] = Convert.ToString(ViewState["ParamCatalog"]);
        Context.Items["ParamTitle"] = Convert.ToString(ViewState["ParamTitle"]);
        Context.Items["ParamContent"] = Convert.ToString(ViewState["ParamContent"]);
        Context.Items["ParamCommand"] = Convert.ToString(ViewState["ParamCommand"]);

        // 传递分页编号
        Context.Items["ParamPageIndex"] = GridView1.PageIndex;

        // 页面跳转至更新画面
        // Server.Transfer("~/App/Master/CommonDictionaryUpdForm.aspx", true);
        ControllerUtils.Transfer(this.Context, "~/App/Master/CommonDictionaryUpdForm.aspx", true);
    }
}
